package com.cloud.upms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.lock.annotation.Lock4j;
import com.cloud.common.core.result.Page;
import com.cloud.common.core.result.Result;
import com.cloud.common.log.annotation.SysLog;
import com.cloud.common.log.enums.OperateTypeEnum;
import com.cloud.common.satoken.core.domain.LoginUser;
import com.cloud.api.upms.model.SysUser;
import com.cloud.api.upms.domain.user.UserAddReq;
import com.cloud.api.upms.domain.user.UserListReq;
import com.cloud.api.upms.domain.user.UserUpdateReq;
import com.cloud.upms.service.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;
import java.util.List;

/**
 * Author:  xuanning
 * Date:    2022/10/29 2:02
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class SysUserController {

    private final SysUserService sysUserService;

    /**
     * 获取用户信息
     */
    @GetMapping(value = "/user/info")
    public Result<LoginUser> info(String username) {
        log.info("username====="+username);
        return sysUserService.getUserInfo(username);
    }

    /**
     * 新增
     * @return
     */
    @PostMapping(value = "/user/add")
    @SaCheckPermission(value = "user:add", orRole = "admin")
    @SysLog(title = "用户管理",operate = OperateTypeEnum.ADD)
    @Lock4j(keys={"#paramReq.userName"})
    public Result<?> add(@Valid @RequestBody UserAddReq paramReq) {
        sysUserService.add(paramReq);
        return Result.success();
    }

    /**
     * 更新
     * @return
     */
    @PostMapping(value = "/user/update")
    @SaCheckPermission(value = "user:update", orRole = "admin")
    @SysLog(title = "用户管理",operate = OperateTypeEnum.UPDATE)
    public Result<?> update(@Valid @RequestBody UserUpdateReq paramReq) {
        sysUserService.update(paramReq);
        return Result.success();
    }

    /**
     * 列表
     * @return
     */
    @PostMapping(value = "/user/list")
    @SysLog(title = "用户管理",operate = OperateTypeEnum.QUERY)
    public Result<?> listUser(@Valid @RequestBody UserListReq paramReq) {
        Page page=paramReq.getPage();
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        List<SysUser> list=sysUserService.listUser(paramReq);
        return Result.page(list,page,new PageInfo(list).getTotal());
    }


    /**
     * 是否禁用
     * @return
     */
    @GetMapping(value = "/user/enable")
    @SaCheckPermission(value = "user:enable", orRole = "admin")
    @SysLog(title = "用户管理",operate = OperateTypeEnum.ENABLE)
    public Result<?> enable(Long id,Integer status) {
        sysUserService.enable(id,status);
        return Result.success();
    }


}
